import Reflux from 'reflux';
import Actions from './Actions';
import update from 'react-addons-update';
import { Toast } from 'antd-mobile';
import Utils from '../../../../utils/index'
import DB from '../../../../app/db';
import nattyStorage from 'natty-storage';
import StorageUtils from '../../../../app/storage';
import {Control} from "react-keeper";
const { API_URL} = require(`./../../../../config/${ENV}.json`);

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    userInfo: {},
    proprietor: {},
    files: []
  },

  customInit() {
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getUser()}});
    let proprietor = StorageUtils.getStorageItem('proprietor');
    this.data.proprietor = update(this.data.proprietor, {$set: {...proprietor}});
    if (proprietor.imageUrl) {
      this.data.files = update(this.data.files, {$set: [{url: API_URL + proprietor.imageUrl}]})
    }
    this.updateComponent();
  },

  onRecordChange(val) {
    this.data.proprietor.record = update(this.data.proprietor.record, {$set: val[0]});
    this.updateComponent();
  },

  onIntroTextChange(val) {
    this.data.proprietor.synopsis = update(this.data.proprietor.synopsis, {$set: val});
    this.updateComponent();
  },

  addZippedImg(files) {
    // let newFiles = this.data.files.concat(files);
    console.log(files);
    this.data.files = update(this.data.files, {$set: files});
    this.updateComponent();
  },

  removeImage() {
    this.data.files = update(this.data.files, {$set: []});
    this.updateComponent();
  },

  saveIntro() {
    let t = this;
    let image = t.data.files && t.data.files[0] && t.data.files[0].image;
    let param = {
      proprietorId: t.data.proprietor.id,
      record: t.data.proprietor.record && t.data.proprietor.record[0],
      synopsis: t.data.proprietor.synopsis,
      ...image
    };
    DB.WechatAPI.updIntro(param).then(
      function (content) {
        if (content) {
          DB.WechatAPI.getProprietorByOpenId({openId: t.data.userInfo.openId}).then(
            function (content) {
              if (content && content.id) {
                StorageUtils.setStorage('proprietor', content);
                if (content.imageUrl) {
                  t.data.files = update(t.data.files, {$set: [{url: API_URL + content.imageUrl}]});
                } else {
                  t.data.files = update(t.data.files, {$set: []});
                }
                t.updateComponent();
              } else {
                StorageUtils.removeStorageItem('proprietor');
                Control.replace('/xq/register', null);
              }
            },
            function (error) {
              StorageUtils.updateUser({proprietorId: ''});
              StorageUtils.removeStorageItem('proprietor');
              Control.replace('/xq/register', null);
            }
          ).catch(function (reason) {
            console.log(reason);
          });
          Toast.success('保存成功！');
        } else {
          Toast.fail('保存失败！')
        }
      },
      function (error) {
        console.log(error);
      }
    );
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});

